package cn.wolfcode.wolf2w.business.controller;

import cn.wolfcode.wolf2w.business.util.SmsUtil;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.common.core.utils.uuid.UUID;
import cn.wolfcode.wolf2w.common.redis.service.RedisService;
import cn.wolfcode.wolf2w.common.redis.util.RedisKeys;
import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/sms")
public class SmsController {

    @Autowired
    private RedisService redisService;
    @PostMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone){
        //生成验证码 5位的
        //String code = UUID.randomUUID().toString().replace("-", "").substring(0,5);
        Random random = new Random();
        Integer rand=random.nextInt(9000000)+100000;
        String code = rand.toString();
        //SmsUtil.sendSmsAli(phone,code);
        System.err.println("验证码："+code);
        //存储验证码(redis)
        String key = RedisKeys.VERIFY_CODE.join(phone);
        long time = RedisKeys.VERIFY_CODE.getTime();
        redisService.setCacheObject(key,code,time, TimeUnit.SECONDS);
        return R.ok();
    }

}
